package cn.tedu.mimall.product.service.impl;

import cn.tedu.mimall.product.ex.ServiceException;
import cn.tedu.mimall.product.mapper.AddressMapper;
import cn.tedu.mimall.product.pojo.dto.AddressAddNewDTO;
import cn.tedu.mimall.product.pojo.dto.AddressUpdateDTO;
import cn.tedu.mimall.product.pojo.entity.Address;
import cn.tedu.mimall.product.pojo.vo.AddressStandardVO;
import cn.tedu.mimall.product.service.IAddressService;
import cn.tedu.mimall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 处理收货地址的业务实现类
 *
 * @author 无所事事的小鱼人
 * @version 0.0.1
 */
@Slf4j
@Service
public class AddressServiceImpl implements IAddressService {

    @Autowired
    AddressMapper addressMapper;

    @Override
    public void addAddress(AddressAddNewDTO addressAddNewDTO) {
        log.debug("开始处理【添加收货地址】的业务，参数：{}",addressAddNewDTO);
        if(!addressAddNewDTO.getPhone().matches("^1[3-9]\\d{9}$")){
            String message="添加收货地址失败，手机号码格式不正确";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,message);
        }
        Address address=new Address();
        BeanUtils.copyProperties(addressAddNewDTO,address);
        int row = addressMapper.insert(address);
        if(row!=1){
            String message="添加收货地址失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【删除收货地址】的业务，参数：{}",id);
        int count = addressMapper.countById(id);
        if(count!=1){
            String message="删除数据失败，地址数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int row = addressMapper.deleteById(id);
        if(row!=1){
            String message="删除收货地址失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }

    @Override
    public void updateInfoById(Long id,AddressUpdateDTO addressUpdateDTO) {
        log.debug("开始处理【修改收货地址】的业务，参数：{},{}",id,addressUpdateDTO);
        AddressStandardVO query = addressMapper.getStandardById(id);
        if(query==null){
            String message="修改收货地址失败，数据不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        if(!addressUpdateDTO.getPhone().matches("^1[3-9]\\d{9}$")){
            String message="添加收货地址失败，手机号码格式不正确";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,message);
        }
        Address address=new Address();
        BeanUtils.copyProperties(addressUpdateDTO,address);
        address.setId(id);
        int row = addressMapper.updateById(address);
        if(row!=1){
            String message="修改收货地址失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

}
